package com.ruoyi.kenuo.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.kenuo.domain.KernelMyMeasure;
import com.ruoyi.kenuo.service.IKernelMyMeasureService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 我的测量Controller
 *
 * @author polo tang
 * @date 2023-09-05
 */
@RestController
@RequestMapping("/kenuo/measure")
public class KernelMyMeasureController extends BaseController
{
    @Autowired
    private IKernelMyMeasureService kernelMyMeasureService;

    /**
     * 查询我的测量列表
     *
     */
   // @PreAuthorize("@ss.hasPermi('kenuo:measure:list')")
    @GetMapping("/list")
    public TableDataInfo list(KernelMyMeasure kernelMyMeasure)
    {
        startPage();
        List<KernelMyMeasure> list = kernelMyMeasureService.selectKernelMyMeasureList(kernelMyMeasure);
        return getDataTable(list);
    }

    /**
     * 导出我的测量列表
     */
    @PreAuthorize("@ss.hasPermi('kenuo:measure:export')")
    @Log(title = "我的测量", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, KernelMyMeasure kernelMyMeasure)
    {
        List<KernelMyMeasure> list = kernelMyMeasureService.selectKernelMyMeasureList(kernelMyMeasure);
        ExcelUtil<KernelMyMeasure> util = new ExcelUtil<KernelMyMeasure>(KernelMyMeasure.class);
        util.exportExcel(response, list, "我的测量数据");
    }

    /**
     * 获取我的测量详细信息
     */
    @PreAuthorize("@ss.hasPermi('kenuo:measure:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(kernelMyMeasureService.selectKernelMyMeasureById(id));
    }

    /**
     * 新增我的测量
     */
    //@PreAuthorize("@ss.hasPermi('kenuo:measure:add')")
    @Log(title = "我的测量", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody KernelMyMeasure kernelMyMeasure)
    {
        String clientUname = kernelMyMeasure.getClientUname();
        String docname = kernelMyMeasure.getDoctorUname();
        Integer sex = kernelMyMeasure.getSex();
        if (StringUtils.isNotEmpty(clientUname)&&StringUtils.isNotEmpty(docname)){
            //根据clientUname doctorUname 查询是否有记录
            KernelMyMeasure kme = new KernelMyMeasure();
            kme.setClientUname(clientUname);
            kme.setDoctorUname(docname);
            if(null!=sex){
                kme.setSex(sex);
            }
            List<KernelMyMeasure> list = kernelMyMeasureService.findKernelMyMeasureList(kme);
            if(!list.isEmpty()){
                return AjaxResult.error("The note name already exists");
            }

        }else{
            return AjaxResult.error("the parameter 'clientUname' or 'doctorUname' cannot be empty");
        }

        return toAjax(kernelMyMeasureService.insertKernelMyMeasure(kernelMyMeasure));
    }

    /**
     * 修改我的测量
     */
   // @PreAuthorize("@ss.hasPermi('kenuo:measure:edit')")
    @Log(title = "我的测量", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody KernelMyMeasure kernelMyMeasure)
    {
        String clientUname =kernelMyMeasure.getClientUname();
        String docUname = kernelMyMeasure.getDoctorUname();
        Integer sex = kernelMyMeasure.getSex();
        if(StringUtils.isNotEmpty(clientUname)){
            KernelMyMeasure kme = new KernelMyMeasure();
            kme.setClientUname(clientUname);
            if(null!=sex){
                kme.setSex(sex);
            }
            if(StringUtils.isNotEmpty(docUname)){
                kme.setDoctorUname(docUname);
            }
            List<KernelMyMeasure> list = kernelMyMeasureService.findKernelMyMeasureList(kme);
            if(!list.isEmpty()){
                return AjaxResult.error("The note name already exists");
            }
        }
        return toAjax(kernelMyMeasureService.updateKernelMyMeasure(kernelMyMeasure));
    }

    /**
     * 删除我的测量
     */
    @PreAuthorize("@ss.hasPermi('kenuo:measure:remove')")
    @Log(title = "我的测量", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(kernelMyMeasureService.deleteKernelMyMeasureByIds(ids));
    }
    /**
     * 根据id删除我的测量
     */
    @GetMapping("/reMid")
    public AjaxResult reMid(Long id)
    {
        String itd = id+"";
        if(StringUtils.isNotEmpty(itd)&&id>0){
            return toAjax(kernelMyMeasureService.deleteKernelMyMeasureById(id));
        }else {
            return AjaxResult.error("id is null or empty,please double check");
        }

    }

}
